<?php

require_once ('BaseAjaxModule.php');

class AdminModule extends BaseAjaxModule {
	
	public function init()	{
		$this->view = new Joob_View_Ajax();
		$this->addFilter(new Joob_Filter_Editor());
		$this->setupUser();
	}
	
	public function preDispatch()	{
		$user = $this->getUser();
		if ($user == null)	{
			throw new Exception("Permission denied");
		}
		if ($user->role != 100)	{
			throw new Exception("Permission denied. Not Admin");
		}
	}
	
	public function editCategoryTask()	{
		if (!$this->getRequest()->hasParam('name'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Name not defined'));
			return;
		}
		
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		
		$id = $this->getParam('id');
		$name = $this->getParam('name');
		$category = new Model_Category();
		$category->id = $id;
		$category->name = $name;
		$this->view->result = $category->editName();
	}
	
	public function editProductTask()	{
		if (!$this->getRequest()->hasParam('name'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Name not defined'));
			return;
		}
		
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		
		$product = new Model_Product();
		$product->id = $this->getParam('id');
		$product->name = $this->getParam('name');
		$product->description = $this->getParam('description');
		$this->view->result = $product->editDetail();
	}
	
	public function getUploadImage($param, $folder)	{
		if (!isset($_FILES[$param]))
			return false;
		$fd = $_FILES[$param];
		$tmpName = $fd['tmp_name'];
		$type = $fd['type'];
		if (!substr($type, 0, 5) == "image")	{
			return false;
		}
		$type = substr($type, 6);
		$data = file_get_contents($tmpName);
		$filename = sha1(rand().time()."Sarah").".".$type;
		$fh = fopen($folder.'/'.$filename, "w");
		fwrite($fh, $data);
		fclose($fh);
		return $filename;
	}
	
	public function editProductImageTask()	{
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		
		$filename = $this->getUploadImage('image', 'static/images/products');
		if ($filename == false)	{
			$this->view->setAjaxResponse($this->createErrorResult('Cannot upload!'));
			return;
		}

		$product = new Model_Product();
		$product->id = $this->getParam('id');
		$product->image = $filename;
		$this->view->result = $product->editImage();
	}
	
	public function editCategoryImageTask()	{
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		
		$filename = $this->getUploadImage('image', 'static/images/products');
		if ($filename == false)	{
			$this->view->setAjaxResponse($this->createErrorResult('Cannot upload!'));
			return;
		}

		$product = new Model_Category();
		$product->id = $this->getParam('id');
		$product->image = $filename;
		$this->view->result = $product->editImage();
	}
	
	public function addNewCategoryTask()	{
		if (!$this->getRequest()->hasParam('name'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Name not defined'));
			return;
		}
		$pro = new Model_Category();
		$pro->name = $this->getParam('name');
		$pro->description = $this->getParam('description');
		$pro->parent_id = $this->getParam('parent_id');
		$this->view->result = $pro->addNew();
	}
	
	public function addNewProductTask()	{
		if (!$this->getRequest()->hasParam('name'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Name not defined'));
			return;
		}
		$pro = new Model_Product();
		$pro->name = $this->getParam('name');
		$pro->description = $this->getParam('description');
		$pro->category_id = $this->getParam('category_id');
		$pro->is_new = $this->getParam('is_new');
		$pro->is_featured = $this->getParam('is_featured');
		$this->view->result = $pro->addNew();
	}
	
	public function updateProductTask()	{
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$product = new Model_Product();
		$product->id = $id;
		$result = $product->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('Product not exist!'));
			return;
		}
		
		$updates = array('name', 'category_id', 'description', 'is_new', 'is_featured');
		
		foreach ($updates as $key) {
			$value = $this->getParam($key);
			if ($value != null)	{
				$product->$key = $value;
			}
		}
		
		$this->view->result = $product->updateProduct();
	}
	
	public function deleteProductTask(){
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$product = new Model_Product();
		$product->id = $id;
		$result = $product->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('Product not exist!'));
			return;
		}
		$this->view->result = $product->delete();
	}
	
	public function addNewAgentTask()	{
		if (!$this->getRequest()->hasParam('name'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Name not defined'));
			return;
		}
		$agent = new Model_Agent();
		$agent->name = $this->getParam('name');
		$agent->region = $this->getParam('region');
		$agent->address = $this->getParam('address');
		$agent->phone = $this->getParam('phone');
		$agent->city = $this->getParam('city');
		$this->view->result = $agent->addNew();
	}
	
	public function deleteAgentTask(){
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$agent = new Model_Agent();
		$agent->id = $id;
		$result = $agent->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('Agent not exist!'));
			return;
		}
		$this->view->result = $agent->delete();
	}

	public function updateAgentTask()	{
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$agent = new Model_Agent();
		$agent->id = $id;
		$result = $agent->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('Agent not exist!'));
			return;
		}
		
		$updates = array('name', 'address', 'phone', 'city', 'region');
		
		foreach ($updates as $key) {
			$value = $this->getParam($key);
			if ($value != null)	{
				$agent->$key = $value;
			}
		}
		
		$this->view->result = $agent->updateAgent();
	}
	
	public function updateConfigTask()	{
		$config = new Model_Config();
		$config->id = 1;
		$result = $config->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('config not exist!'));
			return;
		}
		
		$updates = array('intro', 'contact', 'agent_info', 'footer');
		
		foreach ($updates as $key) {
			$value = $this->getParam($key);
			if ($value != null)	{
				$config->$key = $value;
			}
		}
		
		$this->view->result = $config->updateConfig();
	}

	public function updateNewsTask()	{
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$news = new Model_News();
		$news->id = $id;
		$result = $news->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('news not exist!'));
			return;
		}
		
		$updates = array('news_title', 'content');
		
		foreach ($updates as $key) {
			$value = $this->getParam($key);
			if ($value != null)	{
				$news->$key = $value;
			}
		}
		
		$this->view->result = $news->updateNews();
	}

	public function addNewsTask()	{
		if (!$this->getRequest()->hasParam('news_title'))	{
			$this->view->setAjaxResponse($this->createErrorResult('news_title not defined'));
			return;
		}
		$news = new Model_News();
		$news->news_title = $this->getParam('news_title');
		$news->content = $this->getParam('content');
		$news->since = time();
		$this->view->result = $news->addNew();
	}
	
	public function deleteNewsTask(){
		$id = $this->getParam('id');
		if ($id == null)	{
			$this->view->setAjaxResponse($this->createErrorResult('ID not defined'));
			return;
		}
		$news = new Model_News();
		$news->id = $id;
		$result = $news->connect();
		if (!$result)	{
			$this->view->setAjaxResponse($this->createErrorResult('News not exist!'));
			return;
		}
		$this->view->result = $news->delete();
	}
	
	public function updateAccountTask()	{
		$user = $this->getUser();
		$user->pwd = $this->getParam('oldpwd');
		$result = $user->login();
		if($result === true){
			if($this->getRequest()->hasParam('newpwd')){
				$user->pwd = sha1($this->getParam('newpwd'));
				
			}
			if($this->getRequest()->hasParam('email')){
				$user->email = $this->getParam('email');
				
			}
			
			$this->view->result = $user->updateAccount();
		}
		else{
			$this->view->setAjaxResponse($this->createErrorResult($result));
		}
	}
}

?>